<?php
/**
 * User: 高振
 * Date: 14-4-28
 * Time: 下午3:27
 * Description：
 *
 * sucess:  0
 * nologin: 1
 *
 */

require 'Common.php';

try
{
    $operator = isset($_REQUEST['operator'])?$_REQUEST['operator']:null;
    $size = '';
    //$editorUrlParam = '__HOME__';
    switch($operator)
    {
        //运营后台
        case "manage":
            $uid = isset($_REQUEST['uid'])?$_REQUEST['uid']:false;
            $uid = $uid > 0 ? $uid : false;
            /*if(!$uid)
            {
                throw new Exception("no_login",0);
            }*/
            $role = isset($_REQUEST['role'])?$_REQUEST['role']:1;
            $size = $_POST['size'];
            //$editorUrlParam = '__ADMIN__';
            break;
        case 'findPwd':
            $uid = $_SESSION['find_uid'];
            if(!$uid){
                throw new Exception("no_login",0);
            }
            break;
        case "user":
            if(!$_SESSION['uid']){
                throw new Exception("no_login",0);
            }

            $uid = $_REQUEST['uid'];
            $is_admin = $_REQUEST['is_admin'];
            if($is_admin == false && !$uid){
                $uid  = $_SESSION['uid'];
            }

            $role = 1;
            break;

        case 'remote':
            $akey = $_POST['akey'];
            $skey = $_POST['skey'];
            $sig = $_POST['sig'];
            $uid = $_POST['uid'];

            if(empty($uid) || empty($akey) || empty($skey) || empty($sig))
            {
                throw new Exception('invalid_request_params_wrong');
            }

            $config_key = C('UPLOAD.REMOTE_KEY');
            if($akey!=$config_key){
                throw new Exception('invalid_request_key');
            }

            $key = params_deal(SITE_URL);
            $secret = get_secret($akey,$skey);
            $new_sig = get_signature($key,$secret);
            if($sig!=$new_sig){
                throw new Exception('invalid_request_sig_wrong');
            }
            break;

        case 'remotes':
            $akey = $_POST['akey'];
            $skey = $_POST['skey'];
            $sig = $_POST['sig'];
            $uid = $_POST['uid'] ? $_POST['uid'] : 0;

            if(empty($akey) || empty($skey) || empty($sig))
            {
                throw new Exception('invalid_request_params_wrong');
            }

            $config_key = C('UPLOAD.REMOTE_KEY');
            if($akey!=$config_key){
                throw new Exception('invalid_request_key');
            }

            $key = params_deal(SITE_URL);
            $secret = get_secret($akey,$skey);
            $new_sig = get_signature($key,$secret);
            if($sig!=$new_sig){
                throw new Exception('invalid_request_sig_wrong');
            }
            break;

        case 'regenerate':
            $akey = $_POST['akey'];
            $skey = $_POST['skey'];
            $sig = $_POST['sig'];
            $role = $_POST['role'];
            empty($role) && $role = 1;

            if(empty($akey) || empty($skey) || empty($sig))
            {
                throw new Exception('invalid_request_params_wrong');
            }

            $config_key = C('UPLOAD.REMOTE_KEY');
            if($akey!=$config_key){
                throw new Exception('invalid_request_key');
            }

            $key = params_deal(SITE_URL);
            $secret = get_secret($akey,$skey);
            $new_sig = get_signature($key,$secret);
            if($sig!=$new_sig){
                throw new Exception('invalid_request_sig_wrong');
            }
            unset($akey,$skey,$sig,$config_key,$key,$secret,$new_sig);
            break;

        case 'qrcode':
            $akey = $_POST['akey'];
            $skey = $_POST['skey'];
            $sig = $_POST['sig'];
            $uid = $_POST['uid'];

            if(empty($uid) || empty($akey) || empty($skey) || empty($sig))
            {
                throw new Exception('invalid_request_params_wrong');
            }

            $config_key = C('UPLOAD.REMOTE_KEY');
            if($akey!=$config_key){
                throw new Exception('invalid_request_key');
            }

            $key = params_deal(SITE_URL);
            $secret = get_secret($akey,$skey);
            $new_sig = get_signature($key,$secret);
            if($sig!=$new_sig){
                throw new Exception('invalid_request_sig_wrong');
            }
            break;

        case 'move':
            $akey = $_POST['akey'];
            $skey = $_POST['skey'];
            $sig = $_POST['sig'];
            $uidFaceStr = $_POST['uidFaceStr'];

            if(empty($uidFaceStr) || empty($akey) || empty($skey) || empty($sig))
            {
                throw new Exception("invalid_request_params_wrong",0);
            }

            $config_key = C('UPLOAD')['remote_key'];
            if($akey!=$config_key){
                throw new Exception("invalid_request_key",0);
            }

            $key = params_deal(SITE_URL);
            $secret = get_secret($akey,$skey);
            $new_sig = get_signature($key,$secret);
            if($sig!=$new_sig){
                throw new Exception("invalid_request_sig_wrong",0);
            }
            break;
        case 'wx_face':
            $uid = isset($_REQUEST['uid'])?$_REQUEST['uid']:false;
            $faceUrl = isset($_REQUEST['url'])?$_REQUEST['url']:false;
            if(!$uid){
                throw new Exception("no_login",0);
            }
            if(!$faceUrl){
                throw new Exception("no face url",0);
            }
            break;
        case 'reason_upload':
            $uid = isset($_REQUEST['uid'])?$_REQUEST['uid']:false;
            if(!$uid){
                throw new Exception("no_login",0);
            }
            break;

        default:
            throw new Exception("operator_error",0);
    }
    $do = isset($_REQUEST['do'])?$_REQUEST['do']:null;
    $doType = isset($_REQUEST['do_type'])?$_REQUEST['do_type']:null;
    if(!$do || !$doType || !in_array($do,C('IMG_MANAGE_ACTION')))
    {
        throw new Exception("upload param error!",0);
    }
    $callback = isset($_REQUEST['callback'])?$_REQUEST['callback']:null;

    switch($do)
    {
        //上传
        case "upload":
            $redirect_url = $_REQUEST['redirect'];//回调地址
            $uploadObj  = new Upload();
            $input_field = $_REQUEST['input_field'];
            $filetype = $_REQUEST['filetypes'];
            $is_admin = $_REQUEST['is_admin'];
            $ret = $uploadObj->upload($doType,$uid,$role,2,$input_field,$filetype,$size,$is_admin);
            $redirect_url = str_replace('%s',urlencode(json_encode($ret)),$redirect_url);
            header('Location:'.$redirect_url);
            break;

        //编辑器组件上传
        case "upload_editor":
            $input_field = $_REQUEST['input_field'];
            $redirect = $_REQUEST['redirect'];
            empty($redirect) && $redirect = C('TMPL_PARSE_STRING')['__HOME__'];
            $uploadObj  = new Upload();
            $ret = $uploadObj->upload($doType,$uid,$role,2,$input_field);
            header('Location:'.$redirect.'/Index/uploadredirect?ret='.urlencode(json_encode($ret)));
            break;

        //编辑器文件上传
        case 'upload_editor_file':
            $input_field = $_REQUEST['input_field'];
            $redirect = $_REQUEST['redirect'];
            empty($redirect) && $redirect = C('TMPL_PARSE_STRING')['__HOME__'];
            $uploadObj  = new Upload();
            $ret = $uploadObj->upload($doType,$uid,$role,2,$input_field);
            header('Location:'.$redirect.'/Index/uploadredirect?ret='.urlencode(json_encode($ret)));
            break;

        //普通上传
        case "upload_common":
            $uploadObj  = new Upload();
            $ret = $uploadObj->upload($doType,$uid,$role);
            header('Location:'.C('TMPL_PARSE_STRING')['__HOME__'].'/Home/Return?ret='.base64_encode(json_encode($ret)).'&callback='.$callback);
            break;

        //普通上传face
        case "upload_common_face":
            $uploadObj  = new Upload();
            $ret = $uploadObj->upload_common_face($doType,$uid,$role);
            header('Location:'.C('TMPL_PARSE_STRING')['__HOME__'].'/Home/Return?ret='.base64_encode(json_encode($ret)).'&callback='.$callback);
            break;
        case 'upload_face_by_bytes':
            $photo = $_POST['photo'];
            if(empty($photo)){
                throw new Exception("upload param error!",0);
            }

            $uploadObj  = new Upload();
            $ret = $uploadObj->uploadFaceByBytes($doType,$uid,$photo,$role);
            break;
        //二维码生成
        case 'create_qrcode':
            $url = $_POST['url'];
            if(empty($url)){
                throw new Exception("upload param error!",0);
            }

            $uploadObj  = new Upload();
            $ret = $uploadObj->creadeQrcode($doType,$uid,$url,$role);
            break;
       //上传头像
        case "upload_face":
                $imgdeg = $_REQUEST['imgdeg'];
                $imgcrop = explode(',',$_REQUEST['imgcrop']);
                $w = $imgcrop[2];
                $h = $imgcrop[3];
                $x = $imgcrop[0];
                $y = $imgcrop[1];
                $photo = $_REQUEST['photo'];
                $uploadObj  = new Upload();
                $ret = $uploadObj->upload_face($doType,$uid,$photo,$imgdeg,$w,$h,$x,$y);
                header('Location:'.C('TMPL_PARSE_STRING')['__HOME__'].'/Index/return_iframe?ret='.base64_encode(json_encode($ret)).'&callback='.$callback);
            break;
        //删除
        case "delete":
                if(!isset($_REQUEST['filepaths']))
                {
                    throw new Exception("upload param error!",0);
                }
                $filePaths = urldecode($_REQUEST['filepaths']);
                $filePathArr = explode('|',$filePaths);
                $delObj  = new Del();
                $ret = $delObj->delete($doType,$uid,$role,$filePathArr);
            break;

        // 上传远程文件
        case 'uploadRemote':
            $fileArray = array();
            $fileString = $_POST['filepaths'];

            if(!$fileString)
            {
                throw new Exception("文件列表为空",0);
            }
            $fileArray = explode('|',$fileString);
            $upload  = new Upload();
            $ret = $upload->upload_remote($doType,$uid,$fileArray);
            break;

        //重新生成缩略图
        case 'regenerate':
            $imgData = $_POST['imgData'];
            $imgArr = json_decode(base64_decode($imgData));
            $sizeData = $_POST['sizeData'];
            $sizeArr = $sizeData ? json_decode(base64_decode($sizeData)) : false;
            unset($imgData,$sizeData);

            $upload  = new Upload();
            $ret = $upload->regenerate(base64_decode($doType),$imgArr,$role,$sizeArr);
            unset($imgData,$imgArr,$upload,$role,$do,$doType,$callback,$operator);
            break;

        //重命名
        case 'rename':
            $upload  = new Upload();
            $old = trim($_POST['old']);
            $new = trim($_POST['new']);

            if(empty($old) || empty($new)){
                throw new Exception("新文件名或原文件名为空",0);
            }

            $ret = $upload->rename($doType,$uid,$old,$new);
            break;

        //移动文件
        case 'move':
            $upload  = new Upload();

            $origin_uid = trim($_POST['origin_uid']);
            $new_uid = trim($_POST['new_uid']);
            $filename = trim($_POST['filename']);

            if(empty($filename)){
                throw new Exception("文件名为空",0);
            }

            $ret = $upload->move($doType,$origin_uid,$new_uid,$filename);
            break;

        case 'moveByMuiti':
            $moveArr = json_decode(base64_decode($_POST['move']),true);

            if(empty($moveArr)){
                throw new Exception("参数错误",0);
            }

            $upload = new Upload();
            $ret = $upload->moveByMuiti($moveArr);
            break;

        //上传后跳转到指定的target，图片普通上传
        case 'uploadRedirect':
            $callback = $_REQUEST['callback'];

            $upload = new Upload();
            if($uid==''){
                $uid = false;
            }
            $ret = $upload->upload($doType,$uid,$role);
            header('Location:'.C('TMPL_PARSE_STRING')['__ADMIN__'].'/Index/return_iframe?ret='.base64_encode(json_encode($ret)).'&callback='.$callback);
            break;
        case 'urlUpload':
            $url = $_REQUEST['url'];
            if(empty($url)){
                throw new Exception("url empty",0);
            }
            $upload = new Upload();
            $ret = $upload->uploadImgForUrl($doType,$uid,$url);
            echo json_encode($ret);exit;
            break;
        case 'reasonUpload':
            $media = $_REQUEST['media'];
            if(empty($media)){
                throw new Exception("reason empty",0);
            }
            $upload = new Upload();
            $ret = $upload->uploadReason($doType,$uid,$media);
            echo json_encode($ret);exit;
        default:
    }
    json_exit($ret,$callback);
}
catch(Exception $e)
{
    $error = require('Error.php');
    json_exit(array('msg'=>$e->getMessage(),'code'=>$error[$e->getCode()]));
}